﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace IF.Utils
{
    /// <summary>
    /// 用于推送订单
    /// </summary>
    public class MaoyanUtil
    {
        /// <summary>
        /// <para>
        /// 1、推送订单
        /// </para>
        /// </summary>
        /// <param name="dtro">订单数据</param>
        /// <returns>结果</returns>
        public static string CancelOrderPushDataParam()
        {
            Dictionary<string, object> dicReq = new Dictionary<string, object>();
            string ntid = "1638827052";
            string ooid = "130820033057205221";
            string onoid = "130820039801796761";
            dicReq.Add("tid", ntid);//平台订单编号s
            dicReq.Add("trade_status", "90");//30已付款待发货
            dicReq.Add("pay_status", "0");//付款状态:0未付款1部分付款2已付款
            dicReq.Add("delivery_term", 4);//发货条件:1款到发货 2货到付款(包含部分货到付款)
            dicReq.Add("trade_time", Convert.ToDateTime("2015-07-21 10:57:30"));//推送时间
            dicReq.Add("pay_time", "");//支付时间
            dicReq.Add("buyer_nick", "美团网");//网名
            dicReq.Add("buyer_email", "");//..
            dicReq.Add("pay_id", "");
            dicReq.Add("pay_account", "");//省份	城市	区 北京 北京 大兴区
            dicReq.Add("receiver_name", "赵燕");
            dicReq.Add("receiver_province", "北京");
            dicReq.Add("receiver_city", "北京");
            dicReq.Add("receiver_district", "大兴区");
            dicReq.Add("receiver_address", "不要的地址");
            dicReq.Add("receiver_mobile", "13011111111");
            dicReq.Add("receiver_telno", "");
            dicReq.Add("receiver_zip", "");
            dicReq.Add("logistics_type", "ZTO");
            dicReq.Add("invoice_type", 0);
            dicReq.Add("invoice_title", "");
            dicReq.Add("buyer_message", "");
            dicReq.Add("seller_memo", "");
            dicReq.Add("seller_flag", "0");
            dicReq.Add("post_amount", 0);
            dicReq.Add("cod_amount", "0");
            dicReq.Add("ext_cod_fee", "0");
            dicReq.Add("paid", 0);

            //decimal goodAmount = orderData.ListOrderDetail.Select((p) => Convert.ToDecimal(p.RealPrice) * Convert.ToInt32(p.Num)).Sum();
            //decimal goodAmount = 100;
            //goodAmount = Math.Round(goodAmount, 2, MidpointRounding.AwayFromZero);
            //decimal discount = -(Convert.ToDecimal(100) - goodAmount - Convert.ToDecimal(0));//总的折扣
            List<object> detailList = new List<object>();
            ////int num = orderData.ListOrderDetail.Count;+
            //int num = 1;//商品数量
            int index = 0;
            //decimal adddiscountAmount = 0;
            ////折扣需要进行分摊
            ////foreach (var item in orderData.ListOrderDetail)
            ////{
            //decimal discountAmount = 0;
            //if (index == num - 1)
            //{
            //    discountAmount = discount - adddiscountAmount;
            //}
            //else
            //{
            //    discountAmount = (((Convert.ToDecimal(100) * Convert.ToDecimal(1)) / goodAmount *
            //                       discount));
            //    discountAmount = Math.Floor(discountAmount * 100) / 100;
            //}
            //adddiscountAmount += discountAmount;



            var datail1 = new
               {
                   oid = ooid,
                   num = 1,
                   price = 28,
                   status = 90,
                   refund_status = 0,
                   goods_id = "",
                   spec_id = "",
                   goods_no = "ZYTM01",// item.GoodNo,
                   spec_no = "ZYTMO1O1",// item.MerchantNo,//.Remove(0, item.GoodNo.Length),
                   goods_name = "",
                   spec_name = "",
                   adjust_amount = 0,
                   discount = 3,
                   share_discount = 0,
                   cid = ""

               };

            var datail = new
            {
                oid = onoid,
                num = 1,
                price = 28,
                status = 90,
                refund_status = 0,
                goods_id = "",
                spec_id = "",
                goods_no = "ZYTM01",// item.GoodNo,
                spec_no = "ZYTM0101",// item.MerchantNo,//.Remove(0, item.GoodNo.Length),
                goods_name = "",
                spec_name = "",
                adjust_amount = 0,
                discount = 3,
                share_discount = 0,
                cid = ""

            };

            var datail2 = new
            {
                oid = "130820079590842172",
                num = 1,
                price = 28,
                status = 90,
                refund_status = 0,
                goods_id = "",
                spec_id = "",
                goods_no = "ZYTM01",// item.GoodNo,
                spec_no = "ZYTMO1O1",// item.MerchantNo,//.Remove(0, item.GoodNo.Length),
                goods_name = "",
                spec_name = "",
                adjust_amount = 0,
                discount = 3,
                share_discount = 0,
                cid = ""

            };
            detailList.Add(datail);
            index++;
            //}
            dicReq.Add("order_list", detailList);

            List<object> list = new List<object>();
            list.Add(dicReq);

            //转成JSon
            string json = JsonConvert.SerializeObject(list);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("sid", WDTUtil.SID);
            dic.Add("appkey", WDTUtil.APPKEY);
            dic.Add("timestamp", WDTUtil.DateTimeToStamp(DateTime.Now).ToString());
            dic.Add("shop_no", WDTUtil.SHOPNO);
            dic.Add("trade_list", json);
            string resutl = WDTUtil.SendPost("/openapi2/trade_push.php", dic);
            //解析 json
            JObject obj_ProductJson = JObject.Parse(resutl);
            //状态成功
            int resultCode = Convert.ToInt32(obj_ProductJson["code"].ToString());
            //新增数量
            int orderCount = Convert.ToInt32(obj_ProductJson["new_count"].ToString());

            return resutl;
        }
    }
}
